package idiet.dialog;

import idiet.Activator;
import idiet.bean.CustomerPal;
import idiet.service.CustomerPalService;

import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DialogAddCustomerPal extends TitleAreaDialog {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("idiet.dialog.messages"); //$NON-NLS-1$
	private  Logger logger =Logger.getLogger(DialogAddCustomerPal.class);
	private CustomerPalService palService=(CustomerPalService)Activator.getApplicationContext().getBean("customerPalService");
	private Text textName;
	private Text textComment;

	public DialogAddCustomerPal(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.CLOSE | SWT.MIN | SWT.TITLE);
		// TODO Auto-generated constructor stub
		
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage(BUNDLE.getString("DialogAddCustomerPal.this.message")); //$NON-NLS-1$
		// TODO Auto-generated method stub
		setTitle(BUNDLE.getString("DialogAddCustomerPal.this.title")); //$NON-NLS-1$
		Composite composite =(Composite) super.createDialogArea(parent);
		composite.setLayout(new GridLayout(1, false));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText(BUNDLE.getString("DialogAddCustomerPal.lblNewLabel.text")); //$NON-NLS-1$
		
		textName = new Text(composite_1, SWT.BORDER);
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		textName.addModifyListener(new ModifyListener(){

			@Override
			public void modifyText(ModifyEvent e) {
				// TODO Auto-generated method stub
				if(textName.getText().length()==0){
					setErrorMessage(BUNDLE.getString("DialogAddUnit.no.name.error"));
				}else{
					setErrorMessage(null);
					//setMessage(BUNDLE.getString("DialogAddCustomerPal.this.message"));
					getButton(OK).setEnabled(true);
				}
			}
			
		});
		Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblNewLabel_1.setText(BUNDLE.getString("DialogAddCustomerPal.lblNewLabel_1.text")); //$NON-NLS-1$
		
		textComment = new Text(composite_1, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData gd_textComment = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textComment.heightHint = 68;
		textComment.setLayoutData(gd_textComment);
		new Label(composite_1, SWT.NONE);
		
		Label lblNewLabel_2 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText(BUNDLE.getString("DialogAddUnit.lblsizeIsLimited.text")); //$NON-NLS-1$
		return composite;
	}

	@Override
	protected void cancelPressed() {
		// TODO Auto-generated method stub
		super.cancelPressed();
	}

	@Override
	protected void okPressed() {
		// TODO Auto-generated method stub
		try {
			if(this.textName.getText().length()!=0){
				CustomerPal pal=new CustomerPal();
				pal.setName(this.textName.getText());
				pal.setComment(this.textComment.getText());
				palService.createCustomerPal(pal);
				if(logger.isDebugEnabled()){
					logger.debug("create unit : "+pal.toString());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
			e.printStackTrace();
		}
		super.okPressed();
	}
	
}
